package lanou3g.com.Fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

/**
 * Created by dllo on 2015/10/30.
 */
public class Main_Fragment extends Fragment{

    private static final String ARGS_POSITION = "pasition";
    private static SparseArray<Fragment>fragmentSparseArray = new SparseArray<>();

    public static Fragment newInstance(int position) {
        Fragment f = fragmentSparseArray.get(position);
        if ((f == null)){
            switch (position){
                case 0:
                    f = new Home_Page_Fragment();
                    Bundle bundle_home_page = new Bundle();
                    bundle_home_page.putInt("position",position);
                    f.setArguments(bundle_home_page);
                    break;
                case 1:
                    f = new DialogFragment();
                    Bundle bundle_delicious = new Bundle();
                    bundle_delicious.putInt("position", position);
                    f.setArguments(bundle_delicious);
                case 2:
                    f = new Individual_Fragment();
                    Bundle bundle_individual = new Bundle();
                    bundle_individual.putInt("position",position);
                    f.setArguments(bundle_individual);
            }
            fragmentSparseArray.put(position,f);
        }
        return f;
    }
    private Context context;
    private TextView textView;
    private int position;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context = context;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle args = getArguments();
        position = args.getInt(ARGS_POSITION);
    }
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return new TextView(context);

    }

    /**
     * 初始化控件
     *
     * @param view
     * @param savedInstanceState
     */
    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        textView = (TextView) view;
    }

    /**
     * 初始化数据
     *
     * @param savedInstanceState
     */
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        textView.setText(position + "");
    }
}
